<?php
/**
 * 设计团队
 */
namespace app\admin\controller;

use think\Request;
use app\admin\model\Material as MaterialModel;

class Material extends Base
{
    protected $model;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this->model = new MaterialModel();
    }

    public function index($min_time = null, $max_time = null, $top = null, $status = null, $search = null)
    {
        $map = [];

        if (!empty($min_time)) {
            $map['mtime'] = ['>=', strtotime($min_time)];
            $this->assign('min_time', $min_time);
        }

        if (!empty($max_time)) {
            $map['mtime'] = ['<=', strtotime($max_time)];
            $this->assign('max_time', $max_time);
        }

        if (!empty($top)) {
            $map['top'] = $top;
            $this->assign('top', $top);
        }

        if (!empty($status)) {
            $map['status'] = $status;
            $this->assign('status', $status);
        }

        if (!empty($search)) {
            $map['title'] = ['like','%'.$search.'%'];
            $this->assign('search', $search);
        }

        $counts_page = 8;

        $counts = $this->model->where($map)->count('id');

        $pages = intval(ceil($counts/$counts_page));

        $data = $this->model->where($map)
            ->field('id,title,thumb,cid,top,status,sort,clicks,mtime')
            ->paginate($counts_page);

        $this->assign('data', $data);
        $this->assign('counts', $counts);
        $this->assign('pages', $pages);
        return $this->fetch();
    }

    public function add()
    {
        if (Request::instance()->isPOST()) {
            // var_dump(input("post."));
            // exit;
            $param = input('');
            $title = trim($param['title']);
            
            if (!$title) {
                $this->error('标题不可为空');
            }

            $content = isset($param['editorValue']) ? $param['editorValue'] : '';

            try {
                $this->model->save([
                    'cid' => intval($param['cid']),
                    'title' => $title,
                    'keywords' => $param['keywords'],
                    'desc' => $param['desc'],
                    'thumb' => $param['thumb'],
                    'sort' => intval($param['sort']),
                    'content' => $content,
                    'mtime' => time(),
                    ]);
            } catch (\Exception $e) {
                $msg = $e->getMessage();
                
                $this->error("错误代码:" . $msg);
            }
            $this->success(config('msg.1003'), 'index');
        }
        
        // $categories = model('category')->where('pid', $this->pcid)->where('status', 1)->column('id, name');

        // $this->assign('categories', $categories);
        return $this->fetch();
    }

    public function edit($id)
    {
        $data = $this->model->find($id);

        if (Request::instance()->isPOST()) {
            // var_dump(input("post."));
            // exit;
            $param = input('');
            $title = trim($param['title']);
            
            if (!$title) {
                $this->error('标题不可为空');
            }

            $content = isset($param['editorValue']) ? $param['editorValue'] : '';

            try {
                $this->model->save([
                    'cid' => intval($param['cid']),
                    'title' => $title,
                    'keywords' => $param['keywords'],
                    'desc' => $param['desc'],
                    'thumb' => $param['thumb'],
                    'sort' => intval($param['sort']),
                    'clicks' => intval($param['clicks']),
                    'top' => intval($param['top']),
                    'status' => intval($param['status']),
                    'content' => $content,
                    'mtime' => time(),
                ], ['id'=>$id]);
            } catch (\Exception $e) {
                $msg = $e->getMessage();
                
                $this->error("错误代码:" . $msg);
            }
            $this->success(config('msg.1003'), 'index');
        }

        // $categories = model('category')->where('pid', $this->pcid)->where('status', 1)->column('id, name');

        // $this->assign('categories', $categories);
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function del($id = null)
    {
        if (Request::instance()->isAjax()) {
            if ($this->model->destroy($id)) {
                return ['code'=>2,'msg'=>config('msg.1003')];
            } else {
                $this->error(config('msg.1004'));
            }
        }
    }

    /**
     * topHome 置顶首页状态改变
     */
    public function topHome($id = '', $column = '', $value = 0)
    {
        if (Request::instance()->isAjax()) {
            // return ['code'=>0,'msg'=>'操作失败'];
            if ($this->model->save([$column => $value], ['id' => $id])) {
                return ['code'=>2, 'msg'=>config('msg.1003')];
            } else {
                return ['code'=>0,'msg'=>config('msg.1004')];
            }
        }
    }

    /**
     * 文章审核
     *
     * @param integer $id
     * @param integer $status
     * @return void
     */
    public function duty($id, $status = 0)
    {
        if (Request::instance()->isAjax()) {
            $time = time();

            $result = $this->model->save([
                'status'=>$status,
                'mtime'=>$time,
            ], ['id' => $id]);

            if ($result===false) {
                return ['code'=>0,'msg'=>config('msg.1004')];
            } else {
                return ['code'=>2, 'msg'=>config('msg.1003')];
            }
        }
    }
}
